<?php
class ActivityController 
{

	static $instance;

	private function __construct() {

	}

	protected function __clone() {

	}

	static public function getInstance() {
		if(is_null(self::$instance))
		{
			self::$instance = new self();
		}
		return self::$instance;
	}

	function hasPlaces($activityId)
	{
		$hasPlaces = true;

		$strSQL = "SELECT
						COUNT(student_vs_activity.ACTIVITY_ID) AS TOTAL,
						activity.PLACES
				  FROM 
				 		student_vs_activity 
				  INNER JOIN  	activity  		
				  ON 
				  	activity.ID = student_vs_activity.ACTIVITY_ID
				  WHERE 
				  		student_vs_activity.ACTIVITY_ID = ?
				  GROUP BY 
				  		student_vs_activity.ACTIVITY_ID";



		$objRs =	GlobalApp::getInstance()->executeSQL($strSQL, array($activityId));

		if ( !$objRs->EOF )
		{
			if ( $objRs->fields["TOTAL"] == $objRs->fields["PLACES"]  )
			{
				$hasPlaces = false;
			}
		}

		return $hasPlaces;
	}

	function getActivityType()
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM activity_type ORDER BY NAME ");
		return $objRs;
	}

	function getActivitySchedule()
	{
		$arrActivitySchedule = array();

		$rsActivityType = $this->getActivityType();

		while (!$rsActivityType->EOF)
		{
			$arrActivitySchedule = self::bindArrayIndexedByActivityType( $arrActivitySchedule, $rsActivityType );

			$rsActivityType->MoveNext();
		}

		return $arrActivitySchedule;
	}

	private function bindArrayIndexedByActivityType($arrActivitySchedule, $rsActivityType)
	{
		$activityId   = $rsActivityType->fields["ID"];
		$activityName = $rsActivityType->fields["NAME"];

		if (!(array_key_exists($activityName, $arrActivitySchedule)))
		{
			$arrActivitySchedule[$activityName] =  array();
		}
			
		$arrActivitySchedule[ $activityName ] = self::generateArrayIndexedByActivityDays( $activityId );

		return $arrActivitySchedule;
	}

	private function generateArrayIndexedByActivityDays($activityId)
	{
		$rsActivitiesByType = $this->getActivitiesByType( $activityId );

		$arrActivityDays = array();

		while (!$rsActivitiesByType->EOF)
		{
			$currentActivityDay = $rsActivitiesByType->fields["DAYS"];
				
			if (!(array_key_exists($currentActivityDay,$arrActivityDays)))
			{
				$arrActivitySchedule[ $currentActivityDay ] =  array();
			}

			$arrActivityDays[ $currentActivityDay ][] =  $rsActivitiesByType->fields;


			$rsActivitiesByType->MoveNext();
		}

		return $arrActivityDays;
	}

	function getActivityDays()
	{
		$arrDates = array();
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT DAYS FROM activity order by days");
		while (!$objRs->EOF)
		{
			if (!(in_array($objRs->fields["DAYS"], $arrDates)))
			{
				$arrDates[] = $objRs->fields["DAYS"];

			}

			$objRs->MoveNext();
		}


		return $arrDates;

	}

	function getActivity($ActivityId)
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM activity WHERE ID = ?", array($ActivityId));
		return $objRs;
	}

	function getActivityStatus()
	{
		$strSQL = "SELECT
							activity.*,
						COUNT(student_vs_activity.ACTIVITY_ID) AS TOTAL
						
				  FROM 
				 		 activity
				   INNER JOIN student_vs_activity  		
				  ON 
				  	activity.ID = student_vs_activity.ACTIVITY_ID

				  GROUP BY 
				  		student_vs_activity.ACTIVITY_ID";



		$objRs =	GlobalApp::getInstance()->executeSQL($strSQL);		
		

		return $objRs;
	}

	function getActivities()
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM activity order by days");
		return $objRs;
	}

	function getStudentsInActivity($ActivityId)
	{
		$strSQL = "SELECT student.*,  student_vs_activity.IS_BOUGHT 
						
				  FROM 
				 		 student
				   INNER JOIN student_vs_activity  		
				  ON 
				  	student.ID = student_vs_activity.STUDENT_ID
				  	WHERE student_vs_activity.ACTIVITY_ID = ?
		";
		
		$objRs =	GlobalApp::getInstance()->executeSQL($strSQL, array($ActivityId));
		
		return $objRs;
	}
	
	function getActivitiesByStudent($studentId)
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM student_vs_activity WHERE STUDENT_ID = ?", array($studentId));
		return $objRs;
	}


	function getActivitiesByType($typeActivite)
	{
		$objRs =	GlobalApp::getInstance()->executeSQL("SELECT * FROM activity WHERE ACTIVITY_TYPE_ID = ? order by days", array( $typeActivite ) );
		return $objRs;
	}

}
?>